﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBee : MonoBehaviour {
    public static EnemyBee con;

    public void Awake()
    {
        con = this;
    }
    [Header("跟随的物体")]
    public Transform target;
    [Header("偏移量")]
    public Vector3 offset;
    [Header("移动速度")]
    public float speed;
    public LayerMask layer;

    public Transform Cpoint;
    public Transform Lpoint;
    public Transform Rpoint;
    public Transform bulletPre;
    public Transform Plantpot;

    private bool Collid;
    
    private Animator an;

    public bool isShot;

   
    // Use this for initialization
    void Start()
    {
        an = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        Collid = Physics2D.OverlapCircle(Cpoint.position, 60, layer);
        if (Collid)
        {
            Vector3 vector3 = new Vector3(target.position.x + offset.x, -10 , transform.position.z);
            transform.position = Vector3.Lerp(transform.position, vector3, Time.deltaTime * speed);
            if (!isShot)
            {
                an.SetTrigger("Attack");
                isShot = true;
                InvokeRepeating("Shot", 1.75f, 2.0f);
            }
        }
        else
        {
            isShot = false;
            an.SetTrigger("Idle");
            CancelInvoke("Shot");
        }
    }

    public void Shot()
    {
        Instantiate(bulletPre, Cpoint.transform.position, Cpoint.transform.rotation,Plantpot);
    }

    public void Die()
    {
        CancelInvoke("Shot");
        an.SetTrigger("Hit");
        Destroy(gameObject, 2f);
    }
}
